package com.voxel.sense.infomsg.widget.map;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.InfoWindowSiteItemBean;
import com.voxel.sense.infomsg.data.bean.MakerInfoBean;
import com.voxel.sense.infomsg.data.bean.MultiItemBean;
import com.voxel.sense.infomsg.data.bean.RealDataBean;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;
import com.voxel.sense.infomsg.widget.map.adpter.CustomInfoRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.constants.Constants;

/**
 * Created by ChenYiYao on 2018/8/27.
 * 地图模块，公司信息底部弹窗
 */
public class MapInfoPopWindows extends BottomSheetDialog {

    private RecyclerAdapter mAdapter;
    private TextView tv_name;
    private TextView tv_adress;
    private TextView tv_empty_tip;
    private RecyclerView recycle_view;

    public MapInfoPopWindows(@NonNull Context context) {
        super(context, 0);
        //设置自定义视图
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_map_marker_info, null);
        tv_name = inflate.findViewById(R.id.tv_name);
        tv_adress = inflate.findViewById(R.id.tv_adress);
        tv_empty_tip = inflate.findViewById(R.id.tv_empty_tip);
        initRecycler(inflate);
        setContentView(inflate);
        //是否可取消
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        //去除外围的灰色遮罩
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);


        //内容处于背景
//        getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);

        //设置初始显示高度
        BottomSheetBehavior<View> from = BottomSheetBehavior.from((View) inflate.getParent());
//        from.setPeekHeight(SizeUtils.dp2px(165));

    }


    /**
     * 初始化recyclerView
     *
     * @param root
     */
    private void initRecycler(View root) {
        recycle_view = root.findViewById(R.id.recycle_view);
        if (recycle_view != null) {
            recycle_view.setLayoutManager(new LinearLayoutManager(root.getContext()));
            recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_10dp_white));
            mAdapter = new CustomInfoRecyclerAdapter();
            recycle_view.setAdapter(mAdapter);
            List list = makeData();
            mAdapter.replace(list);
        }
    }

    private List makeData() {
        tv_name.setText("公司名称");
        List<MultiItemBean> listDatas = new ArrayList<>();
        String[] str = {"2018-07-18 18:41:00", "HG345123466", "废水排放"};
        InfoWindowSiteItemBean siteItemData = new InfoWindowSiteItemBean();
        siteItemData.setSiteCode("HG345123466");
        siteItemData.setDatetime("公司名称");
        siteItemData.setType("废水排放");
        MultiItemBean bean = new MultiItemBean(siteItemData, Constants.DES_TYPE);
        listDatas.add(bean);
        RealDataBean[] beans = new RealDataBean[2];
        RealDataBean bean1 = new RealDataBean("mg/l", "总磷", 0, "" + 0.178, "1");
        RealDataBean bean2 = new RealDataBean("mg/l", "pH值", 0, "" + 7.067292, "9");
        beans[0] = bean1;
        beans[1] = bean2;
        RealDataBean bean3 = new RealDataBean("mg/l", "化学需氧(COD)", 0, "" + 24.901703, "80");
        RealDataBean[] beans1 = new RealDataBean[2];
        beans1[0] = bean3;

        MultiItemBean mBean2 = new MultiItemBean(beans, Constants.NORMAL_TYPE);
        MultiItemBean mBean3 = new MultiItemBean(beans1, Constants.NORMAL_TYPE);
        MultiItemBean mBean4 = new MultiItemBean("58", Constants.FACILITY_TYPE);

        listDatas.add(mBean2);
        listDatas.add(mBean3);
        listDatas.add(mBean2);
        listDatas.add(mBean3);
        listDatas.add(mBean2);
        listDatas.add(mBean3);
        listDatas.add(mBean2);
        listDatas.add(mBean3);
        listDatas.add(mBean2);
        listDatas.add(mBean3);
        listDatas.add(mBean2);
        listDatas.add(mBean3);
        listDatas.add(mBean2);
        listDatas.add(mBean4);
        listDatas.add(mBean4);
        return listDatas;
    }

    public void show(MakerInfoBean bean) {
        if (bean == null)
            return;

        refreshData(bean);
        show();
    }

    public void refreshData(MakerInfoBean bean) {
        tv_name.setText(bean.getName());
        if (TextUtils.isEmpty(bean.getAdress())) {
            tv_adress.setVisibility(View.GONE);
        } else {
            tv_adress.setVisibility(View.VISIBLE);
            tv_adress.setText(bean.getAdress());
        }
        if (bean.getDatas() == null || bean.getDatas().size() < 1) {
            recycle_view.setVisibility(View.GONE);
            tv_empty_tip.setVisibility(View.VISIBLE);
        } else {
            mAdapter.replace(bean.getDatas());
            recycle_view.setVisibility(View.VISIBLE);
            tv_empty_tip.setVisibility(View.GONE);
        }
    }

//    public void setTitleClick(View.OnClickListener colick) {
//        tv_name.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
//    }

}
